$(function () {
    let userid = localStorage.getItem("userId");
    findById(userid);
})
//加载数据===============================================
function findById(id) {
    $.ajax({
        data:{id: id},
        url:'/back/user/updateUser',
        dataType:'json',
        type:'get',
        success:function (data) {
            setData(data.userModel);
        }
    })
}
function setData(data) {
    $("#username").val(data.username);
    $("#password").val(data.password);
    $("#phone").val(data.phone);
    $("input[name='sex'][value="+ data.sex +"]").attr("checked",true);
    $("#birthday").val(data.birthday);
    // $("input[name='hobby']").val(data.);
    $("input:checkbox[name='hobby']").each(function () {
        let s = data.hobby;
        let hobby=s.split(",");
        for (let i=0;i<hobby.length;i++){
            if ($(this).val() == hobby[i]){
                $(this).attr('checked',true);
                continue;
            }
        }
    })
    $("#introduce").val(data.introduce);

}
//更新数据===================================================
function update() {
    let userid = localStorage.getItem("userId");
    updateById(userid);
}
function updateById(id) {
    let data={
        id:id,
        password:$("#password").val(),
        phone:$("#phone").val(),
        sex:$("input[name='sex']:checked").val(),
        birthday:$("#birthday").val(),
        hobby:hb(),
        introduce:$("#introduce").val(),
    };
    console.log(data);
    $.ajax({
        data:data,
        dataType:'json',
        url:'/back/user/updateUser',
        type:'post',
        success:function (data) {
            // let url = window.location.href;
            data = data.result;
            if (data==1){
                alert("更新成功");
            }else {
                alert("更新失败");
            }
            $("#right").load("/back/html/user/updateUser.html");
        }
    });

}
function hb() {
    let hb=[];
    $("input:checkbox[name='hobby']:checked").each(function () {
        let state = ($(this).prop('checked'));
        if (state){
            hb.push($(this).val());
        }
    });
    hb = hb.join(',');
    return hb;

}
//返回上一页面。。。。。。。。
function back() {
    $("#right").load("/back/html/user/usermanager.html");
}